/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.siagefi.controller;



import br.siagefi.model.Conta;
import br.siagefi.service.ContaService;
import java.io.Serializable;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author Paulo Lavarini
 */

@ManagedBean
@SessionScoped
public class ContaBean implements Serializable{
    
   private Conta conta;
   private final ContaService service;
   private List<Conta> contas;
   
    private int contRegistro;

    public ContaBean() {
        this.conta = new Conta();
        this.service = new ContaService();
        this.carregarLista();
    }
    
     public final void carregarLista(){
         this.contas = service.listar();
    }
    
    public String gravar(){
        service.gravar(this.getConta());
        this.limparFormularioDoJsf();
        this.carregarLista();
        cont();
        return "conta?faces-redirect=true";
    }
    
    public String alterar(Conta conta){
        this.setConta(conta);
        return "incluirConta";
    }
    
     public String remover(){
        service.remover(this.getConta());
        this.limparFormularioDoJsf();
        this.carregarLista();
        cont();
        return "conta";
    }
     
    public String remover(Conta conta){
        service.remover(conta);
        this.limparFormularioDoJsf();
        this.carregarLista();
        cont();
        return "conta";
    }
  
    public void limparFormularioDoJsf(){
        this.conta = new Conta();
    }
    private void cont(){
        contRegistro = 0;
        for (Conta c : contas) {
            contRegistro++;
        }
    }

    public Conta getConta() {
        return conta;
    }

    public void setConta(Conta conta) {
        this.conta = conta;
    }

    public List<Conta> getContas() {
        return contas;
    }

    public void setContas(List<Conta> contas) {
        this.contas = contas;
    }
    
    
    public int getContRegistro() {
        return contRegistro;
    }

    public void setContRegistro(int contRegistro) {
        this.contRegistro = contRegistro;
    }
    
    public void validateNomeIguais(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        if(value != null){
          String nome = value.toString();
        
            for (Conta c : contas) {
                if(nome.equalsIgnoreCase(c.getNome())){
                    throw new ValidatorException(
                            new FacesMessage("Nome já cadastrado!"));
                }
            }
        }
    }

}
